const Controller = require("egg").Controller;

class formTemplateController extends Controller {
  // 获取模板列表
  async formTemplate () {
    const formList = await this.ctx.service.formTemplate.getFormList();
    this.ctx.body = {
      code: 200,
      data: formList,
    };
  }

  // 保存模板
  async saveFormTemplate () {
    const { ctx } = this;
    const { id, formName, formCode, formTemplate } = ctx.request.body;
    const result = await this.ctx.service.formTemplate.saveFormTemplate({
      id,
      formName,
      formCode,
      formTemplate,
    });
    this.ctx.body = {
      code: 200,
      data: result,
    };
  }

  //修改模板
  async modifyForm () {
    const { ctx } = this;
    const { id, formName, formCode, formTemplate } = ctx.request.body;
    const result = await this.ctx.service.formTemplate.modifyForm({
      id,
      formName,
      formCode,
    });
    this.ctx.body = {
      code: 200,
      data: result,
    };
  }

  // 获取模板
  async editorFormTemplate () {
    const { ctx } = this;
    const { id } = ctx.query;
    const formTemplate = await this.ctx.service.formTemplate.editorFormTemplate(
      id
    );
    this.ctx.body = {
      code: 200,
      data: formTemplate,
    };
  }

  // 删除模板
  async deleteFormTemplate () {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const formTemplate = await this.ctx.service.formTemplate.deleteFormTemplate(
      id
    );
    this.ctx.body = {
      code: 200,
      data: formTemplate,
    };
  }
}

module.exports = formTemplateController;
